import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import Home from './views/Home.vue'
const router = new Router({
  routes:[
    {
      path:'/',
      redirect: 'index'
    },
    {
      path:'/',
      name:'home',
      component:Home,
      children:[
        { path: '/index', name:'index', component:()=>import('./views/index/Index.vue'),
          children:[
            { path: '/index', name:'index', component:()=>import('./components/index/VideoList.vue') },
            { path: '/attention', name:'attention', component:()=>import('./views/index/attention.vue') }
          ]
        },
        { path: '/follow', name:'follow', component:()=>import('./views/follow/Follow.vue') },
        { path: '/me', name:'me', component:()=>import('./views/me/me.vue') },
        { path: '/msg', name:'msg', component:()=>import('./views/msg/msg.vue') }
      ]
    },
    {
      path: '/sign',
      name: 'sign',
      component: () =>import('./views/sign/sign.vue')
    },
    {
      path: '/tpsign',
      name: 'tpsign',
      component: () =>import('./views/sign/tpsign.vue')
    },
    {
      path: '/code',
      name: 'code',
      component: () =>import('./views/sign/code.vue')
    },
    {
      path: '/toast',
      name: 'toast',
      component: () =>import('./components/toast/toast.vue')
    },
    {
      path: '/edit',
      name: 'edit',
      component: () =>import('./views/me/edit.vue')
    },
    {
      path: '/publish',
      name: 'publish',
      component: () =>import('./views/publish/publish.vue')
    },
    { path: '/fans', name:'fans', component:()=>import('./views/msg/topbar/fans.vue') },
    { path: '/zan', name:'zan', component:()=>import('./views/msg/topbar/zan.vue') },
    { path: '/my', name:'my', component:()=>import('./views/msg/topbar/my.vue') },
    { path: '/pinlun', name:'pinlun', component:()=>import('./views/msg/topbar/pinlun.vue') },
    { path: '/inmsg', name:'inmsg', component:()=>import('./views/msg/topbar/inmsg.vue') },
    { path: '/followvideo', name:'followvideo', component:()=>import('./components/index/followVideos.vue') }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log(sessionStorage);
  // to: Route: 即将要进入的目标 路由对象
  // from: Route: 当前导航正要离开的路由
  // next: Function: 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。

  const nextRoute = ['msg', 'me', 'publish', 'follow'];
  let Login = JSON.parse(sessionStorage.getItem('login'));// 是否登录
  var isLogin=''
  if (Login==null) {
    isLogin=''
  }else{
    isLogin=Login.login
  }
  // let isLogin = Login.login;
  console.log(isLogin)
  // 未登录状态；当路由到nextRoute指定页时，跳转至login
  if (nextRoute.indexOf(to.name) >= 0) {  
    if (Login==null) {
      // console.log('what fuck');
      router.push({ name: 'sign' })
    }
  }
  // 已登录状态；当路由到login时，跳转至home 
  next();
});

const originalPush = Router.prototype.push
  Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
export default router